Imports System.Data

Partial Class WebForms_Advisor
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage
        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim dao As New DAOMain.CommonDAO
            Dim lfunc As New PolyFunc.ASPFunc

            Dim dt As DataTable = dao.ExecQuery("Select user_name, advisor_name From vw_advisor_mstr Order by advisor_name")

            lfunc.BindDropDown(Advisor_cmb, dt, "advisor_name", "user_name")

            Call InitVars()
            Provider_td.InnerText = ViewState("Provider")
            Provider_td.InnerText = ViewState("Provider")
            Call LoadAdvisor()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If
    End Sub

    Private Sub LoadAdvisor()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_ADV_DTL_DAO
        Dim vo As New DAOMain.POLICY_ADV_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.ADVISOR_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")
        vo.POLICY_ADV_START_DT = cFunc.ParseSetDate(ViewState("SDate"))

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Advisor_cmb.SelectedValue = .Item("ADVISOR_CD")
                StartDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_ADV_START_DT"))
                EndDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_ADV_END_DT"))
                Notes_txt.Text = .Item("POLICY_ADV_REMARKS")
            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        If Trim(EndDate_txt.Text) <> "" Then

            Dim sDate As Date = cFunc.ParseSetDate(StartDate_txt.Text)
            Dim eDate As Date = cFunc.ParseSetDate(EndDate_txt.Text)


            If eDate < sDate Then
                polyFunc.MessageAlert(Me.ParentPage, "Invalid Date Range!")
                Exit Sub
            End If
        End If
        Dim dao As New DAOMain.POLICY_ADV_DTL_DAO
        Dim vo As New DAOMain.POLICY_ADV_DTL_VO
        Dim voCheck As New DAOMain.POLICY_ADV_DTL_VO
        Dim daoCHeck As New DAOMain.POLICY_ADV_DTL_DAO

        Dim dt As DataTable

        With voCheck
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .ADVISOR_CD = Advisor_cmb.SelectedValue.ToString
            .POLICY_ADV_START_DT = cFunc.ParseSetDate(StartDate_txt.Text)

            dt = daoCHeck.Search(voCheck)
        End With

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .ADVISOR_CD = Advisor_cmb.SelectedValue.ToString
            .POLICY_ADV_START_DT = cFunc.ParseSetDate(StartDate_txt.Text)
            .POLICY_ADV_END_DT = cFunc.ParseSetDate(EndDate_txt.Text)
            .POLICY_ADV_REMARKS = Notes_txt.Text

            Select Case dt.Rows.Count
                Case 0
                    .ADVISOR_CD = Advisor_cmb.SelectedValue.ToString
                    .POLICY_ADV_CREA_BY = Session("UserName")
                    .POLICY_ADV_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
                Case Else
                    .ADVISOR_CD = Advisor_cmb.SelectedValue.ToString
                    .POLICY_ADV_LAST_UPD_BY = Session("UserName")
                    .POLICY_ADV_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
    End Sub
End Class
